package com.ie.cloud.payment.config;

import com.ie.cloud.payment.constants.BizStatus;
import com.ie.cloud.payment.exception.BizException;
import com.ie.cloud.payment.exception.MD5Exception;
import com.ie.cloud.payment.exception.RepeatedSubmissionException;
import com.ie.cloud.payment.vo.base.Result;
import org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.AuthenticationException;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created with FR
 *
 * @Author: DQ
 * @Date: 2025/07/17/15:32
 * @Description: 描述语句
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public Result<Object> sysException(BizException e) {
        return Result.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(DateTimeParseException.class)
    @ResponseBody
    public Result<Object> DateTimeParseExceptionH(DateTimeParseException e) {
        return Result.error(BizStatus.DATE_TIME_FORMAT_ERROR);
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public Result<Object> AuthenticationExceptionH(AuthenticationException e) {
        return Result.error(BizStatus.UNAUTHORIZED);
    }

    @ExceptionHandler(MD5Exception.class)
    @ResponseBody
    public Result<Object> MD5ExceptionH(IllegalArgumentException e) {
        return Result.error(BizStatus.MD5_UNAUTHORIZED_ERROR);
    }

    @ExceptionHandler(RepeatedSubmissionException.class)
    @ResponseBody
    public Result<Object> RepeatedSubmissionExceptionH(RepeatedSubmissionException e) {
        return Result.error(BizStatus.REPEATED_COMMIT_ERROR);
    }

    @ExceptionHandler(InvalidContentTypeException.class)
    @ResponseBody
    public Result<Object> InvalidContentTypeExceptionH(InvalidContentTypeException e) {
        return Result.error(BizStatus.INVALID_FORMAT_PARAMETER);
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result<Object> NullPointerExceptionH(NullPointerException e) {
        return Result.error(BizStatus.NULL_POINTER_ERROR);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<Object> MethodArgumentNotValidExceptionH(MethodArgumentNotValidException e) {
        List<String> listMessage = new ArrayList<>(10);
        for (ObjectError allError : e.getAllErrors()) {
            String message = allError.getDefaultMessage();
            listMessage.add(message);
        }
        return Result.error(BizStatus.INTERNAL_ERROR.getCode(),listMessage.toString()
                .replace("[", "").replace("]", ""));
    }

}
